﻿using System;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;

public class GenericValidations
{
    #region local elements
    //name should be aplhanumeric, except '_'
    private static Boolean isAlphaNumeric(String value)
    {
        if (String.IsNullOrWhiteSpace(value))
            return false;

        Regex r = new Regex("^[a-zA-z0-9_]+$");

        if (r.IsMatch(value))
            return true;
        else
            return false;
    }

    //validate date
    private static Boolean isValidDate(String dateString)
    {
        //if empty its a valid date
        if (String.IsNullOrWhiteSpace(dateString))
            return true;

        try
        {
            Convert.ToDateTime(dateString);

            //no time element expected
            if (dateString.Length != GlobalVariables.DateFormat.Length)
            {
                Logger.Show(Logger.INVALID_DATE);
                return false;
            }
        }
        catch (Exception)
        {
            Logger.Show(Logger.INVALID_DATE + ". Date Entered : " + dateString);
            return false;
        }
        return true;
    }
    #endregion

    #region global validations
    //validate from-to dates
    public static Boolean validateFromToDates(String fromDateString, String toDateString)
    {
        //dates are valid or not
        if (!isValidDate(fromDateString)) return false;
        if (!isValidDate(toDateString)) return false;

        //if one is empty then return true, no more validation
        if (String.IsNullOrWhiteSpace(fromDateString) || String.IsNullOrWhiteSpace(toDateString))
            return true;

        //if reached - both dates are present
        DateTime fromDate = Convert.ToDateTime(fromDateString);
        DateTime toDate = Convert.ToDateTime(toDateString);

        //from MUST be greater than to
        if (fromDate >= toDate)
        {
            Logger.Show("TO_DATE must be greater than FROM_DATE.");
            return false;
        }
        return true;
    }

    //name validation
    public static Boolean isValidName(String Name, ListItemCollection existingNames, String mode)
    {
        if (String.IsNullOrWhiteSpace(Name))
            return true;

        //name must contain only charcters, digits and "_"
        if (!GenericValidations.isAlphaNumeric(Name))
        {
            Logger.Show(Logger.INVALID_NAME);
            return false;
        }

        //name must be unique
        ListItem lt = existingNames.FindByText(Name);
        if ((lt != null) && (mode.ToUpper() == "CREATE"))
        {
            Logger.Show(Logger.EXISTING_NAME);
            return false;
        }

        return true;
    }

    //perform string replacesment, replace " with ' and ''' with single ' and finally ' with ''
    public static String convertStringQuotes2SQL(String query)
    {
        query = Regex.Replace(query,"\"", "'");
        query = Regex.Replace(query, "'+", "'");
        query = Regex.Replace(query, "'", "''");
        return query;
    }
    #endregion
}
